<template>
  <div>
    <slot name="title" :title="title">
      <h4 class="company-section-title">{{ title }}</h4>
    </slot>

    <table class="table table-bordered" v-loading="loading">
      <thead>
        <tr class="company-info-bg">
          <th v-for="(c, index) in columns" :key="index">{{ c.label }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-if="!loading && !totalCount">
          <td :colspan="columns.length">
            <div class="text-muted text-center p-3">
              <span>暂无{{ title }}</span>
            </div>
          </td>
        </tr>
        <tr v-for="(item, index) in data" :key="index">
          <td v-for="(c, index) in columns" :key="index">
            <span v-if="c.type === 'index'">{{ index + 1 }}</span>
            <span v-else>{{ item[c.prop || ''] }}</span>
          </td>
        </tr>
      </tbody>
    </table>

    <div class="mt-3 d-flex justify-content-end">
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="index"
        :page-size="size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    usci: String,
    title: String,
    apiView: {
      type: String,
      required: true,
    },
    apiResKey: {
      type: String,
      required: true,
    },
    columns: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      data: [],
      loading: false,
      totalCount: 0,
      index: 1,
      size: 10,
    }
  },
  computed: {
    url() {
      return `/api/xyd/view/${this.apiView}`
    },
  },
  watch: {
    usci: {
      handler: function (val) {
        if (val) {
          this.getData()
        }
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    getData() {
      this.loading = true
      return this.$http
        .get(this.url, {
          params: {
            registerNo: this.usci,
            index: this.index,
            size: this.size,
          },
        })
        .then(res => {
          const data = res.data[this.apiResKey]
          this.data = data.data || []
          this.totalCount = data.totalCount

          this.$emit('get-data', data)
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>

<style lang="scss" scoped></style>
